from django.shortcuts import render, redirect
from django.http import HttpResponse
from functools import wraps
from . import python_sms_send
import DLT.congratulations, SSQ.congratulations

"""
定时启动程序
定时更新系统数据，并检测当前数据库中未核实投注数据是否中奖，如中奖，调用短信通知平台进行通知
"""
from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job

try:
    hour_t = '06'
    minute_t = '00'
    second_t = '00'
    # 实例化调度器
    scheduler = BackgroundScheduler()  # 在后台运行调度，不影响当前的系统计算运行
    # 调度器使用DjangoJobStore()
    scheduler.add_jobstore(DjangoJobStore(), "default")


    # 'cron'方式循环，周一到周五day_of_week='mon-fri'，每天22:30:00执行,id为工作ID作为标记
    @register_job(scheduler, 'cron', day_of_week='mon-sun', hour=hour_t, minute=minute_t, id='pre_analysis')  # 每天自动更新
    def test_job():

        """按模板发送
        msg1 = DLT.congratulations.update_and_check_dict()
        if len(msg1)>0:
            for dic in msg1:
                python_sms_send.send_msg_by_model_DLT(dic)

        msg2 = SSQ.congratulations.update_and_check_dict()
        if len(msg2)>0:
            for dic in msg2:
                python_sms_send.send_msg_by_model_SSQ(dic)
        """

        """
        按短信形式发送，将所有中奖信息放一起
        """
        msg1 = DLT.congratulations.update_and_check()
        msg2 = SSQ.congratulations.update_and_check()
        msg = msg1+msg2
        if len(msg)>0 :
            python_sms_send.send_msg(msg)



    # def test_job():
    #     print('执行任务')

    # scheduler.add_job(test_job, 'cron', day_of_week='mon-sun', hour=hour_t, minute=minute_t, id='lottry_auto_check')

    # 监控任务
    # register_events(scheduler)
    # 调度器开始
    scheduler.start()
except Exception as e:
    print(e)
    scheduler.shutdown()




def index(request):
    http = render(request, 'index.html')
    return HttpResponse(http)
